<template>
  <div class="app-container">
    <div class="  flex-column" style="width: 100%;height: 100%;">
      <div class="flex-between mt10 mb10">
        <div>
          <span class="ml10">标题：</span>
          <el-input style="width: 300px;" v-model="title"></el-input>
        </div>
        <div class="mr10">
          <el-button type="primary" @click="save_content">保存</el-button>
          <el-button type="primary" @click="CloseCurrentTab()">关闭</el-button>
        </div>
      </div>
      <div style="flex: 1;position:relative;">
        <div style="position:absolute;top:0;right:0;bottom:0;left:0;">
          <WangEditor ref="editor" />
        </div>
      </div>
      <div style="height: 20px;"></div>
    </div>
  </div>

</template>

<script>
  import {
    Get_Content,
    Save_Content
  } from "@/api/system/notice";

  export default {
    name: "Editwd",
    components: {
    },
    data() {
      return {
        id: '',
        type: '',
        title: '',
        content: '',
        filename: ''
      }
    },
    created() {
      const id = this.$route.params && this.$route.params.docID;
      this.type = this.$route.params && this.$route.params.type;
      if (id !== '0') {
        this.get_content(id);
      } else {
        this.$nextTick(() => {
          let visitedViews = this.$store.state.tagsView.visitedViews;
          visitedViews.some(v => {
            if (v.path == this.$route.path) {
              v.title =(this.type=='aqkp'? '安全科普':'通知公告') + '-' + "新建";
            }
          })
        });
      }
    },
    methods: {
      /** 获取文档 */
      get_content(id) {

        var params = {
          type: this.type,
          id: id
        }
        Get_Content(params).then(res => {
          this.id = res.id;
          this.title = res.title;
          this.filename = res.filename;
          this.content = res.content;
          this.$nextTick(() => {
            this.$refs.editor.setEditorHtml(this.content)
          });
          let visitedViews = this.$store.state.tagsView.visitedViews;
          visitedViews.some(v => {
            if (v.path == this.$route.path) {
              v.title = (this.type=='aqkp'? '安全科普':'通知公告') + '-' + res.title;
            }
          })
        });

      },
      /** 保存文档 */
      save_content() {
        if (!this.title) {
          this.$message.error("标题不能为空");
          return false;
        }
        const loading = this.loading('保存中...');
        const params = {
          id: this.id,
          title: this.title,
          type: this.type,
          filename: this.filename,
          content: this.$refs.editor.getEditorHtml()
        }
        Save_Content(params).then(res => {
          this.id = res.id;
          this.filename = res.filename;
          this.$modal.msgSuccess("保存成功");
          loading.close();
        }).catch(err => {
          loading.close();
        });
      },


    }
  };
</script>
